package com.thousandsunny.consumer;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;

import static org.springframework.cloud.security.oauth2.client.feign.OAuth2FeignRequestInterceptor.AUTHORIZATION;
import static org.springframework.cloud.security.oauth2.client.feign.OAuth2FeignRequestInterceptor.BEARER;

/**
 * @author Savitar
 */
public class MyOAuth2FeignRequestInterceptor implements RequestInterceptor {

    public static ThreadLocal<Authentication> authenticationThreadLocal = new InheritableThreadLocal<>();

    @Override
    public void apply(RequestTemplate template) {
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authenticationThreadLocal.get().getDetails();
        template.header(AUTHORIZATION, BEARER + details.getTokenValue());
        authenticationThreadLocal.remove();
    }
}

